﻿namespace CRM.DataAccess.DAOs
{
    using System.Collections.Generic;
    using System.Data.Entity;
    using CRM.DataAccess.Entities;
    using System.Linq;

    public class EmailTemplateDAO : DbContext
    {
        public EmailTemplateDAO()
            : base("name=Entities")
        {
        }
        public virtual DbSet<EMAIL_TEMPLATE> EMAIL_TEMPLATES { get; set; }
        private string convertMaiToStringToString(string str)
        {
            return str.Replace("%20", " ").Replace("%3F", "?").Replace("%27", "&").Replace("%3E", "=").Replace("%0D%0A", "\n\n");
        }
        private string convertStringToMailToString(string str)
        {
            return str.Replace(" ", "%20").Replace("?", "%3F").Replace("&", "%27").Replace("=", "%3E").Replace("\n\n", "%0D%0A");
        }
        private EMAIL_TEMPLATE convertEmailTemplateStringToEmailToString(EMAIL_TEMPLATE e)
        {
            e.DESCRIPTION = convertStringToMailToString(e.DESCRIPTION);
            e.MESSAGE = convertStringToMailToString(e.MESSAGE);
            e.SUBJECT = convertStringToMailToString(e.SUBJECT);
            e.TEMPLATE_NAME = convertStringToMailToString(e.TEMPLATE_NAME);
            return e;
        }
        private EMAIL_TEMPLATE convertEmailToStringToEmailTemplateString(EMAIL_TEMPLATE e)
        {
            e.DESCRIPTION = convertMaiToStringToString(e.DESCRIPTION);
            e.MESSAGE = convertMaiToStringToString(e.MESSAGE);
            e.SUBJECT = convertMaiToStringToString(e.SUBJECT);
            e.TEMPLATE_NAME = convertMaiToStringToString(e.TEMPLATE_NAME);
            return e;
        }
        //Get EMAIL_TEMPLATE by condition
        public EMAIL_TEMPLATE GetEmailTemplateByID(long ID)
        {
            try
            {
                var Query = (from c in EMAIL_TEMPLATES
                            where c.ID == ID
                            select c).AsNoTracking().FirstOrDefault<EMAIL_TEMPLATE>();
                EMAIL_TEMPLATE et = Query as EMAIL_TEMPLATE;
                return et;
            }
            catch
            {
                return null;
            }
        }
        //Get EMAIL_TEMPLATE by condition
        public List<EMAIL_TEMPLATE> GetEmailTemplateByCondition(string templateName, string subject, string description)
        {
            try
            {
                var Query = from c in EMAIL_TEMPLATES
                            where ((templateName != "" ? (c.TEMPLATE_NAME.ToUpper().Contains(templateName.ToUpper())) : (true))
                            && (subject != "" ? (c.SUBJECT.ToUpper().Contains(subject.ToUpper())) : (true))
                            && (description != "" ? (c.DESCRIPTION.ToUpper().Contains(description.ToUpper())) : (true)))

                            //c.TEMPLATE_NAME.ToUpper().Contains(templateName == "" ? c.TEMPLATE_NAME.ToUpper() : templateName.ToUpper())
                            //&& c.SUBJECT.ToUpper().Contains(subject == "" ? c.SUBJECT : subject.ToUpper())
                            //&& c.DESCRIPTION.ToUpper().Contains(description == "" ? c.DESCRIPTION : description.ToUpper())
                        //     vi du khi c.subject = "" => luon false nen se khong duoc query
                            select c;
                List<EMAIL_TEMPLATE> cs = Query.ToList();
                if (cs.Count > 0)
                {
                    for (int i = 0; i < cs.Count; i++)
                        cs[i] = convertEmailToStringToEmailTemplateString(cs[i]);
                    return cs;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        //Get All EMAIL_TEMPLATE
        public List<EMAIL_TEMPLATE> ListAll()
        {
            try
            {
                var Query = from c in EMAIL_TEMPLATES
                            select c;
                List<EMAIL_TEMPLATE> cs = Query.ToList();
                if (cs.Count > 0)
                {
                    for (int i = 0; i < cs.Count; i++)
                        cs[i] = convertEmailToStringToEmailTemplateString(cs[i]);
                    return cs;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        //Add new EMAIL_TEMPLATE
        public int addNewEmailTemplate(EMAIL_TEMPLATE email)
        {
            try
            {
                email = convertEmailTemplateStringToEmailToString(email);
                this.EMAIL_TEMPLATES.Add(email);
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        //Update Email Template
        public int UpdateEmailTemplate(EMAIL_TEMPLATE email)
        {
            try
            {
                email = convertEmailTemplateStringToEmailToString(email);
                var original = EMAIL_TEMPLATES.Find(email.ID);
                if (original != null)
                {
                    Entry(original).CurrentValues.SetValues(email);
                    SaveChanges();
                    return 1;
                }
                return -1;
            }
            catch
            {
                return -1;
            }
        }
        //Delete Email template
        public int DeleteEmailTemplate(long emailID)
        {
            try
            {
                this.EMAIL_TEMPLATES.RemoveRange(EMAIL_TEMPLATES.Where(x => x.ID == emailID));
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
    }
}
